package cn.t235.service;

import cn.t235.mapper.SysUserMapper;
import cn.t235.pojo.SysUser;
import cn.t235.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 天天向上 （john.yi@qq.com）
 * @date 2020/10/26.
 */
public class UserService {
    private SysUserMapper userMapper;
    private SqlSession sqlSession;
    public List<SysUser> get(String realName, Integer[] roleIds, int pageNo, int pageSize) {
        List<SysUser> users = null;
        try {
            sqlSession = MyBatisUtil.createSession();
            userMapper = sqlSession.getMapper(SysUserMapper.class);
//            users = userMapper.getByRealNameAndRoleIds(realName, roleIds);
            users = userMapper.getByRealNameAndRoleIds(realName,
                    roleIds == null ? null : Arrays.asList(roleIds),
                    (pageNo - 1) * pageSize, pageSize);
//            Map<String, Object> map = new HashMap<>();
//            map.put("realName", realName);
//            map.put("roleIds", roleIds);
//            users = userMapper.getByRealNameAndRoleIdWithMap(map);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return users;
    }

    public int getCount(String realName, Integer[] roleIds) {
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSession();
            userMapper = sqlSession.getMapper(SysUserMapper.class);
            count = userMapper.getByRealNameAndRoleIdsCount(realName, roleIds == null ? null : Arrays.asList(roleIds));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return count;
    }
}
